import { performance } from 'perf_hooks';
import {redPackBalance, TransferBcosERC20} from './client/bcos/BcosClient.js';
import { GetAssetListByAddressList, GetBalanceOf } from './request/api.js';
import { UUID } from './util/UUIDUtil.js';
import {BigNumber, ethers} from 'ethers';


const concurrency = 200; // 并发数
const iterations = 10; // 测试次数
const startTime = performance.now();

let completedRequests = 0;
let totalRequestTime = 0;


async function runConcurrentTransferRequests() {
  const promises = [];
  for (let i = 0; i < concurrency; i++) {
    promises.push(runSingleTransferRequest(i));
  }
  await Promise.all(promises);
}

async function runSingleTransferRequest(index) {
  for (let i = 0; i < iterations; i++) {
    try {
      // 调用要测试的方法
      const startRequestTime = performance.now();

      const request = {
        userAddress: '0x08ed2b691821d813df4c64a6c76d3fc33958eff8',
        seriesType: '1',
      };
      const result = await GetAssetListByAddressList(request);

      const endRequestTime = performance.now();
      const requestTime = endRequestTime - startRequestTime;
      totalRequestTime += requestTime;
      console.log(`Request ${index + 1} - Iteration ${i + 1} successful. Result:`, result);
      completedRequests++;
    } catch (error) {
      console.error(`Request ${index + 1} - Iteration ${i + 1} failed:`, error);
    }
  }
}


runConcurrentTransferRequests().then(() => {
  const endTime = performance.now();
  const totalTime = endTime - startTime;
  const tps = completedRequests / (totalTime / 1000);
  const averageRequestTime = totalRequestTime / completedRequests;
  console.log(`All requests completed in ${totalTime} milliseconds.`);
  console.log(`Average request time: ${averageRequestTime} milliseconds.`);
  console.log(`TPS: ${tps}`);
});



